import 'package:flutter/material.dart';
import 'package:study_app/scenes/account/account.dart';
import 'package:study_app/scenes/main_parts/main_parts.dart';
import 'package:study_app/scenes/test_use/test_use.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HcdTabbar(),
      builder: (context, child) => Scaffold(
        body: GestureDetector(
          onTap: () {
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus &&
                currentFocus.focusedChild != null) {
              FocusManager.instance.primaryFocus!.unfocus();
            }
          },
          child: child,
        ),
      ),
    );
  }
}

class HcdTabbar extends StatefulWidget {
  const HcdTabbar({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => _HcdTabbarState();
}

class _HcdTabbarState extends State<HcdTabbar> {
  int current = 0;
  List<Widget> pages = [
    const HomePage(),
    const TestUse(),
    const Account()
  ];
  @override
  void initState() {
    super.initState();
    current = 0;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red,
      bottomNavigationBar: _bottomBar(),
      body: IndexedStack(
        index: current,
        children: pages,
      ),
    );
  }

  _bottomBar() {
    var seleColor = const Color(0xff333333);
    var unseleColor = const Color(0xffCCCCCC);
    var unSelectTextStyle = const TextStyle(fontSize: 12);
    var selectedTextStyle = const TextStyle(fontSize: 12);
    return BottomNavigationBar(
      items: _tabbarItems(),
      currentIndex: current,
      backgroundColor: Colors.white,
      type: BottomNavigationBarType.fixed,
      selectedItemColor: seleColor,
      unselectedItemColor: unseleColor,
      selectedLabelStyle: selectedTextStyle,
      unselectedLabelStyle: unSelectTextStyle,
      onTap: (index) {
        _changPage(index);
      },
    );
  }

  List<BottomNavigationBarItem> _tabbarItems() {
    List<BottomNavigationBarItem> tabbarItems = [
      const BottomNavigationBarItem(icon: Icon(Icons.bookmark), label: "首页"),
      const BottomNavigationBarItem(icon: Icon(Icons.account_box), label: "试用"),
      const BottomNavigationBarItem(icon: Icon(Icons.account_box), label: "我的"),
    ];

    return tabbarItems;
  }

  _changPage(int index) {
    if (index != current) {
      setState(() {
        current = index;
      });
    }
  }
}
